package com.cs.paoyer.administration.util;


//import org.apache.struts.action.Action;
import org.apache.struts.action.ActionMapping;
//import org.apache.struts.action.ActionMessage;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//import org.apache.struts.action.ActionMessages;
import java.io.IOException;
import java.util.Vector;

import com.cs.paoyer.administration.business.model.UserVO;
import com.cs.paoyer.common.exception.CommonUserException;
import com.cs.paoyer.common.exception.UserException;
import com.cs.paoyer.administration.exception.AdministrationUserException;

public class AccessControl
{
	public AccessControl()
	{
	}

	public void validateAccess(ActionMapping mapping,  HttpServletRequest request, HttpServletResponse response) throws IOException, UserException
	{
    if(request.getSession().getAttribute("userInfo")==null)
    {
       throw new UserException(new AdministrationUserException().getModuleVO(), 
                      AdministrationUserException.ENDED_SESSION);
    } 
		
		String path= mapping.getPath();
		UserVO user = (UserVO)request.getSession().getAttribute("userInfo");            
		Vector activeResources = new Vector(user.getResources());
		int verify = activeResources.indexOf(path);
    if(verify==-1)
    {      
           throw new UserException(new CommonUserException().getModuleVO(), 
           CommonUserException.NOT_ALLOWED_RESOURCE);
                      
    }
	
	}
  
  public void validateAccess(String path,  HttpServletRequest request, HttpServletResponse response) throws IOException, UserException
	{
    if(request.getSession().getAttribute("userInfo")==null)
    {
       throw new UserException(new AdministrationUserException().getModuleVO(), 
                      AdministrationUserException.ENDED_SESSION);
    } 
		
		
		UserVO user = (UserVO)request.getSession().getAttribute("userInfo");            
		Vector activeResources = new Vector(user.getResources());
		int verify = activeResources.indexOf(path);
    if(verify==-1)
    {      
           throw new UserException(new CommonUserException().getModuleVO(), 
           CommonUserException.NOT_ALLOWED_RESOURCE);
    }
	
	}
}